{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import math\n",
    "import cv2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def rotate_point(x, y, center_x, center_y, rotation_angle):\n",
    "\n",
    "    # Translate the point\n",
    "    translated_x = x - center_x\n",
    "    translated_y = y - center_y\n",
    "\n",
    "    # Rotate the point\n",
    "    theta = math.radians(rotation_angle)\n",
    "    rotated_x = translated_x * math.cos(theta) + translated_y * math.sin(theta)\n",
    "    rotated_y = - translated_x * math.sin(theta) + translated_y * math.cos(theta)\n",
    "\n",
    "    # Translate the point back\n",
    "    x_rotated = rotated_x + center_x\n",
    "    y_rotated = rotated_y + center_y\n",
    "\n",
    "    return x_rotated, y_rotated"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from PIL import Image\n",
    "\n",
    "def rotate_image_minimal_y(image, pixel_x, pixel_y):\n",
    "\n",
    "    # Calculate the rotation angle based on the given pixel's coordinates\n",
    "    center_x = image.width / 2\n",
    "    center_y = image.height / 2\n",
    "    angle_deg = -math.degrees(math.atan2(pixel_y - center_y, pixel_x - center_x))\n",
    "\n",
    "    # Rotate the image\n",
    "    rotated_image = image.rotate(angle_deg, resample=Image.BICUBIC, expand=True)\n",
    "\n",
    "    return rotated_image\n",
    "\n",
    "# Example usage\n",
    "image_path = '/home/$USER/$image_path'\n",
    "\n",
    "\n",
    "image = Image.open(image_path).convert(\"RGB\")\n",
    "image = np.asarray(image)\n",
    "\n",
    "img_width = image.shape[1]\n",
    "img_height = image.shape[0]\n",
    "center_x = 350\n",
    "center_y = img_height / 2\n",
    "\n",
    "pixel_x = 320  # Replace with the x-coordinate of the pixel of interest\n",
    "pixel_y = 170 # Replace with the y-coordinate of the pixel of interest\n",
    "\n",
    "plt.figure()\n",
    "plt.imshow(image)\n",
    "plt.scatter(pixel_x, pixel_y)\n",
    "plt.scatter(center_x, center_y)\n",
    "plt.show()\n",
    "\n",
    "\n",
    "\n",
    "angle_deg = math.degrees(math.atan2(pixel_y - center_y, pixel_x - center_x))\n",
    "print(angle_deg)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "desired_angle = 90\n",
    "rot_angle = desired_angle - angle_deg\n",
    "print(rot_angle)\n",
    "\n",
    "rotation_matrix = cv2.getRotationMatrix2D((center_x, center_y), -rot_angle, 1)\n",
    "rotated_image = cv2.warpAffine(image, rotation_matrix, (img_width, img_height))\n",
    "\n",
    "rot_x, rot_y = rotate_point(pixel_x, pixel_y, center_x, center_y, -rot_angle)\n",
    "plt.figure()\n",
    "plt.imshow(rotated_image)\n",
    "plt.scatter(rot_x, rot_y)\n",
    "plt.scatter(center_x, center_y)\n",
    "angle_deg = math.degrees(math.atan2(rot_y - center_y, rot_x - center_x))\n",
    "print(angle_deg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "gauge_reading",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
